package com.longge;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class AllPathsSourceTarget {

    private List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        LinkedList<Integer> path = new LinkedList<>();
        dfs(graph, 0, path);
        return res;
    }

    private void dfs(int[][] graph, int i, LinkedList<Integer> path) {
        path.add(i);
        if (i == graph.length - 1) {
            res.add(new ArrayList<>(path));
            path.removeLast();
            return;
        }
        for (int s : graph[i]) {
            dfs(graph, s, path);
        }
        path.removeLast();
    }
}
